package com.ztl.ztlcantest;
// MainActivity.java

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;


import androidx.appcompat.app.AppCompatActivity;

import com.ztl.jni.ZtlCanCallback;
import com.ztl.jni.ZtlCanManager;
import com.ztl.layout.GridSpacingItemDecoration;
import com.ztl.layout.MyAdapter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

// MainActivityOnlyCan.java


public class MainActivityOnlyCan extends AppCompatActivity {


    ZtlCanManager mZtlCanManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ztl_init_can();
    }

    // 重要接口1 初始化
    int ztl_init_can(){
        // step 0, use call back to recvive data // 接受数据
        ZtlCanCallback mZtlCanCallback = new ZtlCanCallback() {

            // 数据是byte[]
            @Override
            public void get_recv_can_data(boolean eff, boolean rtr, String canId,
                                          byte[] canData, int dataLen) {
                // eff 是否是拓展帧
                // rtr 是否是远程帧

                // ZtlCanManager.bytesToHex 这个函数 第二个参数是用来配置数据是否空格隔开
                System.out.println("get_recv_can_data log1 len:"+dataLen+"  eff:"+eff + " rtr:"+rtr + " canId:"+canId+ "#"+ZtlCanManager.bytesToHex (canData,null).toUpperCase());

                System.out.println("get_recv_can_data log2 len:"+dataLen+"  eff:"+eff + " rtr:"+rtr + " canId:"+canId+ "#"+ZtlCanManager.bytesToHex (canData," ").toUpperCase());
            }
        };

        mZtlCanManager = new ZtlCanManager(mZtlCanCallback);
        mZtlCanManager.mDebug = false;

        ////////////////////////////////////////////////////////////////
        // 你可以不用初始化ui，直接使用下面的示例。
        // you can just use it without init_ui()
        ////// step 1 设置波特率，打开can口
                ztl_init_can_and_baudrate("can0", 250000);// just call this one time, if the baudrate is the same.
                ztl_init_can_open("can0");

        ////////////////////////////////////////////////////////////////
        // //////step 2 send data 发送数据
        // //////step 2.1 send string demo : cansend can0 789#123456 ； 传递字符串例子
                        String strdata = "123456";
                        int ret = mZtlCanManager.sendCan(0x789, 0x0, 0x0, strdata);
                        System.out.println("send can data ret:" + ret);
        ////////////////////////////////////////////////////////////////
        // ///////step 2.2 send byte buffer demo: cansend can0 123#010203 ； 传递byte buffer例子
                        byte[] data = {0x01,0x02,0x03};
                        int ret2 = mZtlCanManager.sendCan(  0x123, 0, 0, data.length,  data);
                        System.out.println("send can data ret:" + ret2);
        ////////////////////////////////////////////////////////////////
        // //////step 2.3 send byte buffer demo: cansend can0 123456#010203 ；拓展帧例子
                        ret2 = mZtlCanManager.sendCan(  0x123456, 1, 0, data.length,  data);
                        System.out.println("send can data ret:" + ret2);
        ////////////////////////////////////////////////////////////////
        //////step 2.4 send byte buffer demo: cansend can0 123456#R010203 ；远程帧例子
                        ret2 = mZtlCanManager.sendCan(  0x123456, 0, 1, data.length,  data);
                        System.out.println("send can data ret:" + ret2);

        // exit close can
        // // ztl_close_can();
        return 0;
    }

    // 重要接口2 初始化波特率
    int ztl_init_can_and_baudrate(String strCanName , int baudrate ){
        return mZtlCanManager.init(strCanName, baudrate);
    }

    // 重要接口3 打开can
    int ztl_init_can_open(String strCanName){
        if(mZtlCanManager.openCan(strCanName)>=0){
            mZtlCanManager.startCanReceiverThread();
            return 0;
        }else{
            System.out.println("open can failed");
            return -1;
        }
    }

    // 重要接口4 关闭can，这个接口可能会耗时10ms
    void ztl_close_can(){
        mZtlCanManager.closeCan();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        ztl_close_can();
        System.exit(0);
    }
}



